package com.wwwarehouse.common.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.tools.ToastUtils;

public class ChangeEnvActivity extends BaseActivity {
    ListView listView = null;
    String[] envArr = {"DEV环境", "CIT环境", "SIT环境", "UAT环境", "DEV2环境", "CIT2环境", "RELEASE环境", "DEVOPS环境"};
    LayoutInflater mInflater = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_env);

        mInflater = LayoutInflater.from(this);
        listView = findView(R.id.list_view);
        EnvAdapter adapter = new EnvAdapter();
        listView.setAdapter(adapter);
    }

    class EnvAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return envArr.length;
        }

        @Override
        public Object getItem(int position) {
            return envArr[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        Holder holder = null;
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                holder = new Holder();
                convertView = mInflater.inflate(R.layout.view_list_a_layout, null);
                holder.textView = (TextView) convertView.findViewById(R.id.tv_text);
                holder.listItemView = (RelativeLayout) convertView.findViewById(R.id.list_item);
                convertView.setTag(holder);
            }
            else {
                holder = (Holder) convertView.getTag();
            }

            holder.textView.setText(envArr[position]);
            holder.listItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position == 0) {
                        sp.putValue(Constant.sp_Env, Constant.ENV_DEV);
                        ToastUtils.showToast("环境已经切换成DEV");
                    }
                    else if (position == 1) {
                        sp.putValue(Constant.sp_Env, Constant.ENV_CIT);
                        ToastUtils.showToast("环境已经切换成CIT");
                    }
                    else if (position == 2) {
                        sp.putValue(Constant.sp_Env, Constant.ENV_SIT);
                        ToastUtils.showToast("环境已经切换成SIT");
                    } else if (position == 3) {
                        sp.putValue(Constant.sp_Env, Constant.ENV_UAT);
                        ToastUtils.showToast("环境已经切换成UAT");
                    } else if (position == 4){
                        sp.putValue(Constant.sp_Env, Constant.ENV_DEV2);
                        ToastUtils.showToast("环境已经切换成DEV2");
                    }else if (position == 5){
                        sp.putValue(Constant.sp_Env, Constant.ENV_CIT2);
                        ToastUtils.showToast("环境已经切换成CIT2");
                    }
                    else if (position == 6){
                        sp.putValue(Constant.sp_Env, Constant.ENV_RELEASE);
                        ToastUtils.showToast("环境已经切换成RELEASE");
                    }
                    setResult(1);
                    finishActivity();
                }
            });

            return convertView;
        }

        class Holder {
            RelativeLayout listItemView = null;
            TextView textView = null;
        }
    }
}
